<?php

namespace App\Service\Check\KfDataSource;

use App\Enums\Check\CheckEnum;
use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Enums\Pay\RmsPayEnum;
use App\Kuafu\Pay\Model\PayStreamModel;
use App\Library\Rms\RmsPayApi;
use App\Models\Check\CheckStreamModel;
use App\Models\Cruise\CruiseCheckAgainstModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderPayHistoryModel;

class KfOpenShopBankTransfer extends BaseKfDataSource
{
    public function generateLists(int $pageNum, int $pageSize): bool
    {
        $this->lists = OpenShopSignOrderPayHistoryModel::query()
            ->whereBetween('createdAt', [
                $this->checkStreamParam->getStartTime(),
                $this->checkStreamParam->getEndTime()
            ])
            ->where('payStatus',
                OpenShopSignOrderPayHistoryModel::PAY_STATUS_OK)
            ->where(function ($q) {
                $q->where('payChannel', OpenShopSignOrderPayHistoryModel::PAY_CHANNEL_OFFLINE)
                    ->orWhere(function ($q) {
                        $q->whereIn('payChannel', [OpenShopSignOrderPayHistoryModel::PAY_CHANNEL_ALI, OpenShopSignOrderPayHistoryModel::PAY_CHANNEL_WX])
                            ->where('payNo', '线下二维码支付，无流水');
                    });
            })
            ->forPage($pageNum, $pageSize)
            ->get()
            ->map(function ($item) {
                $item['orderId'] = $item['bizId'];

                return $item;
            });

        return (bool)$this->lists->count();
    }

    public function transfer($item): array
    {
        list($company, $companyAccount, $_) = CheckEnum::getBankCompany($item['sysType']);

        return [
            'tradeTime'       => $tradeTime = strtotime($item['createdAt']),
            'date'            => (int)date('Ymd', $tradeTime),
            'orderId'         => $item['orderId'],
            'subject'         => $item['subject'],
            'sysType'         => $item['sysType'],
            'tradePrice'      => $item['payPrice'],
            'orderPrice'      => $item['payPrice'],
            'feePrice'        => 0,
            'feeRate'         => 0,
            'incomePrice'     => $item['payPrice'] - $item['fee'],
            'thirdPayNo'      => '',
            'company'         => $company,
            'companyAccount'  => $companyAccount,
            'payNo'           => $item['payNo'],
            'payChannel'      => $item['payChannel'],
            'payChannelChild' => 0,
            'payAction'       => 0,
            'checkStatus'     => CheckEnum::CHECK_STATUS_SUCCESS, // TODO 等审核功能做了，改待审核
            'errorReason'     => '',
            'remark'          => '',
            'payPic'          => json_encode([picturePath($item['proofImg']),]),
        ];
    }
}
